package com.cpi.cornpp.costelement.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.costelement.exception.CostElementException;
import com.cpi.cornpp.costelement.form.CostElementForm;
import com.cpi.cornpp.costelement.view.CostElementView;
import com.cpi.cornpp.transports.view.TransportView;

public class DeleteCostElementTransportAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String SHOW_NEW_COST_ELEMENT_FORWARD = "showNewCostElement";
	private static final String SHOW_EDIT_COST_ELEMENT_FORWARD = "showEditCostElement";
	private static final String FAILURE_FORWARD = "error";
	private static final String COST_ELEMENT_LIST_FORWARD = "costElementList";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws CostElementException{
		int index = 0;
		CostElementForm costElementForm = null;
		CostElementView costElementView = null;
		TransportView transportView = null;
		TransportView deletedTransportView = null;
		List transportsView = null;
		List deletedCostElementsTransport = null;
		List addedCostElementsTransport = null;
		HttpSession session = null;
		ActionForward forward = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		
		try{
			session = request.getSession(false);
			costElementView = (CostElementView) session.getAttribute("costElementView");
			deletedCostElementsTransport = (List) session.getAttribute("deletedCostElementTransport");
			addedCostElementsTransport = (List) session.getAttribute("addedCostElementTransport");
			
			if ((form != null) && (costElementView != null)){
				costElementForm = (CostElementForm) form;
				transportsView = costElementView.getTransports();
				deletedCostElementsTransport = (CollectionUtils.isNotEmpty(deletedCostElementsTransport)) ? deletedCostElementsTransport : new ArrayList(0);
				
				if ((CollectionUtils.isNotEmpty(transportsView)) && ((costElementForm.getTransportIndex() >= 0) && (costElementForm.getTransportIndex() <= transportsView.size()))){
					deletedTransportView = (TransportView) transportsView.remove(costElementForm.getTransportIndex());
					deletedCostElementsTransport.add(deletedTransportView);
					
					if (CollectionUtils.isNotEmpty(addedCostElementsTransport)){
						
						for (Iterator itr = addedCostElementsTransport.iterator(); itr.hasNext(); ){
							transportView = (TransportView) itr.next();
							
							if (transportView.getTransportIdentifier() == deletedTransportView.getTransportIdentifier()){
								break;
							}
							
							index++;
						}
						
						addedCostElementsTransport.remove(index);
					}
					
					costElementView.setCostElementCode(costElementForm.getCostElementView().getCostElementCode());
					costElementView.setCostElementDescription(costElementForm.getCostElementView().getCostElementDescription());
				}
				
				session.setAttribute("costElementView", costElementView);
				session.setAttribute("deletedCostElementTransport", deletedCostElementsTransport);
				request.setAttribute("costElementView", costElementView);
				
				forward = (costElementForm.getCostElementView().getCostElementID() != -1) ? mapping.findForward(SHOW_EDIT_COST_ELEMENT_FORWARD) : mapping.findForward(SHOW_NEW_COST_ELEMENT_FORWARD);
			} else {
				forward = mapping.findForward(COST_ELEMENT_LIST_FORWARD);
			}
			
		} catch(Exception e){
			CostElementException cee = new CostElementException(e , CostElementException.LAYER_ACTION, CostElementException.ACTION_DELETE);;
			
			session.setAttribute(CUSTOM_EXCEPTION, cee);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw cee;
		}
		
		return forward;
	}
}